/****************************
 * @author Laureta Xhaferaj
 * @email laureta@first.al
 * 
 * form-ajax validation
 */
jQuery(document).ready(function()
{
    $('#res-made').click(function(e) {
        var startdate = $('input[name$="startdate"]').val();
        var starttime = $('input[name$="starttime"]').val();
        var enddate = $('input[name$="enddate"]').val();
        var endtime = $('input[name$="endtime"]').val();
        var e = document.getElementById("classes");
        var classes = e.options[e.selectedIndex].value;
        $.ajax({
            type: 'post',
            url: 'show_reservation',
            data: {
                startdate: startdate,
                starttime: starttime,
                enddate: enddate,
                endtime: endtime,
                classes: classes
            },
            datatype: "json",
            beforeSend: function()
            {
                jQuery('#ajax-loading').show();
            }
        })
                .done(function(data)
                {
                    if (data.success === 1)
                    {

                        var arr = data.values;

                        $('#validation-div').empty();
                        $("#validation-div").removeClass('alert alert-danger').addClass("");

                        if ($('#table-div').children().length !== 0) {
                            $('#dataTable').DataTable().fnDestroy();
                            $('#dataTable').empty();
                            $('#table-div').empty();
                        }

                        str = [];
                        str.push('<table id="dataTable" class="table table-bordered table-condensed table-hover table-striped rent datatable-res"><thead>\n\
                                     <tr><th>ID</th>\n\
                                     <th>Emri</th>\n\
                                     <th>Mbiemri</th>\n\
                                     <th>Tel</th>\n\
                                     <th>Start Date</th>\n\
                                     <th>End Date</th>\n\
                                     <th>Nr Days</th>\n\
                                     <th>Pre Paid</th>\n\
                                     <th>Type</th>\n\
                                     </tr>\n\
                                     </thead><tbody>\n\
                                    ');
                        jQuery.each(arr, function(index, value)
                        {
                            if (value.length !== 0)
                            {
                                str.push('<tr id=' + value.rentalId + '>\n\
                                        <td>' + value.rentalId + '</td>\n\
                                        <td>' + value.drfirstname + '</td>\n\
                                        <td>' + value.drlastname + '</td>\n\
                                        <td>' + value.drhphone + '</td>\n\
                                        <td>' + value.STARTTIME + '</td>\n\
                                        <td>' + value.ENDTIME + '</td>\n\
                                        <td>' + value.res_rentdays + '</td>\n\
                                        <td>' + value.res_prepaid + ' ' + value.curency + '</td>\n\
                                    ');

                                if (value.onlineResId !== null) {
                                    str.push('<td><i class="fa fa-globe"></i></td></tr>');
                                } else {
                                    str.push('<td><i class="fa fa-building-o"></i></td></tr>');
                                }
                            }
                        });
                        str.push('</tbody></table>');
                        document.getElementById("table-div").innerHTML += str.join("");
                        $('#dataTable').DataTable({
                            "bPaginate": true,
                            "bSort": true,
                            "searching": true

                        });
                        $('#dataTable').on('click', 'tbody > tr', function() {
                            var values = $(this).find('td').map(function() {
                                return $(this).text();
                            });
                            var rentalId = $(this).closest('tr').attr('id');
                            window.location.href = 'reservation/edit/' + rentalId + '~' + startdate + '~' + starttime + '~' + enddate + '~' + endtime;
                        });

                        jQuery('#ajax-loading').hide();
                    }

                    else {
                        window.location = data.redirect_to;
                    }
                })
                .fail(function(jqXHR, ajaxOptions, thrownError)
                {
                    alert('Failed');
                });
        return false;
    });
    function initDataTables() {
        $('#dataTable').DataTable({/* put all current options here*/})

    }

    $('#free-cars').click(function(e) {
        var startdate = $('input[name$="startdate"]').val();
        var starttime = $('input[name$="starttime"]').val();
        var enddate = $('input[name$="enddate"]').val();
        var endtime = $('input[name$="endtime"]').val();
        var e = document.getElementById("classes");
        var classes = e.options[e.selectedIndex].value;
        $.ajax({
            type: 'post',
            url: 'free_cars',
            data: {
                startdate: startdate,
                starttime: starttime,
                enddate: enddate,
                endtime: endtime,
                classes: classes
            },
            datatype: "json",
            beforeSend: function()
            {
                jQuery('#ajax-loading').show();
            }
        })
                .done(function(data)
                {
                    if (data.success === 1)
                    {
                        if (data.errors != "") {
                            $('#validation-div').empty()
                            var err = data.errors;
                            jQuery.each(err, function(index, value)
                            {
                                if (value.length !== 0)
                                {
                                    $("#validation-div").addClass('alert alert-danger');
                                    document.getElementById("validation-div").innerHTML +=
                                            '<span class="glyphicon glyphicon-warning-sign"></span>'
                                            + ' ' + value + '<br/>';
                                }
                            });
                            jQuery('#ajax-loading').hide();
                        } else {
                            var arr = data.values;
                            $('#validation-div').empty();
                            $("#validation-div").removeClass('alert alert-danger').addClass("");

                            if ($('#table-div').children().length !== 0) {
                                $('#dataTable').DataTable().fnDestroy();
                                $('#dataTable').empty();
                                $('#table-div').empty();
                            }

                            str = [];
                            str.push('<table id="dataTable" class="table \n\
                                     table-bordered table-condensed table-hover table-striped rent datatable-res"><thead>\n\
                                     <th>ID</th>\n\
                                     <th>Make</th>\n\
                                     <th>Model</th>\n\
                                     <th>License</th>\n\
                                     <th>Milage</th>\n\
                                     <th>Year</th>\n\
                                     <th>Class</th>\n\
                                     </tr>\n\
                                     </thead><tbody>\n\
                                    ');
                            jQuery.each(arr, function(index, value)
                            {
                                if (value.length !== 0)
                                {
                                    str.push('<tr id=' + value.id + '>\n\
                                    <td>' + value.id + '</td>\n\
                                    <td>' + value.Make + '</td>\n\
                                    <td>' + value.Model + '</td>\n\
                                    <td>' + value.License + '</td>\n\
                                    <td>' + value.Milage + '</td>\n\
                                    <td>' + value.year + '</td>\n\
                                    <td>' + value.class + '</td>\n\
                                    </tr>');
                                }
                            });
                            str.push('</tbody></table>');
                            document.getElementById("table-div").innerHTML += str.join("");
                            $('#dataTable').DataTable({
                                "bPaginate": true,
                                "bSort": true,
                                "searching": true

                            });
                            $('#dataTable').on('click', 'tbody > tr', function() {
                                var values = $(this).find('td').map(function() {
                                    return $(this).text();
                                });
                                var id = $(this).closest('tr').attr('id');
                                window.location.href = 'rental/create/' + id + '~' + startdate + '~' + starttime + '~' + enddate + '~' + endtime;
                            });
                            jQuery('#ajax-loading').hide();
                        }
                    }
                    else {
                        window.location = data.redirect_to;
                    }
                })
                .fail(function(jqXHR, ajaxOptions, thrownError)
                {
                    alert('Failed');
                });
        return false;
    });

    $('#res-now').click(function(e) {
        var startdate = $('input[name$="startdate"]').val();
        var starttime = $('input[name$="starttime"]').val();
        var enddate = $('input[name$="enddate"]').val();
        var endtime = $('input[name$="endtime"]').val();
        var e = document.getElementById("classes");
        var classes = e.options[e.selectedIndex].value;
        $.ajax({
            type: 'post',
            url: 'free_cars',
            data: {
                startdate: startdate,
                starttime: starttime,
                enddate: enddate,
                endtime: endtime,
                classes: classes
            },
            datatype: "json",
            beforeSend: function()
            {
                jQuery('#ajax-loading').show();
            }
        })
                .done(function(data)
                {
                    if (data.success === 1)
                    {
                        if (data.errors != "") {
                            $('#validation-div').empty()
                            var err = data.errors;
                            jQuery.each(err, function(index, value)
                            {
                                if (value.length !== 0)
                                {
                                    $("#validation-div").addClass('alert alert-danger');
                                    document.getElementById("validation-div").innerHTML +=
                                            '<span class="glyphicon glyphicon-warning-sign"></span>'
                                            + ' ' + value + '<br/>';
                                }
                            });
                            jQuery('#ajax-loading').hide();
                        }
                        else {
                            var arr = data.values;
                            $('#validation-div').empty();
                            $("#validation-div").removeClass('alert alert-danger').addClass("");

                            if ($('#table-div').children().length !== 0) {
                                $('#dataTable').DataTable().fnDestroy();
                                $('#dataTable').empty();
                                $('#table-div').empty();
                            }

                            str = [];
                            str.push('<table id="dataTable" class="table \n\
                                     table-bordered table-condensed table-hover table-striped rent datatable-res"><thead>\n\
                                     <th>ID</th>\n\
                                     <th>Make</th>\n\
                                     <th>Model</th>\n\
                                     <th>License</th>\n\
                                     <th>Milage</th>\n\
                                     <th>Year</th>\n\
                                     <th>Class</th>\n\
                                     </tr>\n\
                                     </thead><tbody>\n\
                                    ');
                            jQuery.each(arr, function(index, value)
                            {
                                if (value.length !== 0)
                                {
                                    str.push('<tr id=' + value.id + '>\n\
                                    <td>' + value.id + '</td>\n\
                                    <td>' + value.Make + '</td>\n\
                                    <td>' + value.Model + '</td>\n\
                                    <td>' + value.License + '</td>\n\
                                    <td>' + value.Milage + '</td>\n\
                                    <td>' + value.year + '</td>\n\
                                    <td>' + value.class + '</td>\n\
                                    </tr>');
                                }
                            });
                            str.push('</tbody></table>');
                            document.getElementById("table-div").innerHTML += str.join("");
                            $('#dataTable').DataTable({
                                "bPaginate": true,
                                "bSort": true,
                                "searching": true

                            });
                            $('#dataTable').on('click', 'tbody > tr', function() {
                                var values = $(this).find('td').map(function() {
                                    return $(this).text();
                                });
                                var id = $(this).closest('tr').attr('id');
                                window.location.href = 'reservation/create/' + values[0] + '~'
                                        + startdate + ' ' + starttime + '~' + enddate + ' ' + endtime + '';
                            });
                            jQuery('#ajax-loading').hide();
                        }
                    }
                    else {
                        window.location = data.redirect_to;
                    }
                })
                .fail(function(jqXHR, ajaxOptions, thrownError)
                {
                    alert('Failed');
                });
        return false;
    });
});

